package cn.edu.neusoft.yuejian12.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;


import cn.edu.neusoft.yuejian12.JavaBean.Login;
import cn.edu.neusoft.yuejian12.Model.UserModel;
import cn.edu.neusoft.yuejian12.R;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText userpass;
    private Switch save_data;
    private Button btn_login;
    private Button btn_reg;
    private Login loginResult;
    SharedPreferences sp;
    SharedPreferences sp_uid;
    private SharedPreferences.Editor editor;
    private String userid;
    private String error;
    private ImageView image_qq;

    private String openidString;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        initData();
}

    public void initViews()
    {

        username=(EditText) findViewById(R.id.user);
        userpass=(EditText) findViewById(R.id.password);
        btn_login=(Button) findViewById(R.id.btn_login);
        btn_reg=(Button) findViewById(R.id.btn_reg);
        /*save_data=(Switch)findViewById(R.id.switch_compat);*/
      /*  readSP();
*/


    }
    private void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                loginResult = new Login();
                final UserModel userModel=new UserModel();
                retrofit2.Call call=userModel.login(username.getText().toString(),userpass.getText().toString());
                Callback<Login> callback=new Callback<Login>() {
                    @Override
                    public void onResponse(retrofit2.Call<Login> call, Response<Login> response) {

                        loginResult = response.body();
                        /*String userid = loginbean.getId();*/
                        if (response.isSuccessful()) {
                            /*loginResult = response.body();*/
                            userid = loginResult.getId();
                            error = loginResult.getError();
                             if(username.length() == 0||userpass.length() == 0){
                                Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                            }
                           else if (!"用户名或密码不正确".equals(error)) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                                 Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                 intent.putExtra("uid", userid);
                                 sp = getSharedPreferences("loginResult", Context.MODE_PRIVATE);
                                 editor = sp.edit();
                                 editor.putString("uid", userid);
                                 editor.commit();
                                 LoginActivity.this.startActivity(intent);
                                 finish();

                                 //失败处理
                            }
                            else
                                {
                                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();

                            }
                        }
                    }







                    @Override
                    public void onFailure(retrofit2.Call<Login> call, Throwable t) {

                            }

                    };
                    call.enqueue(callback);

                    }
                });

            }





    private void initData() {

    }
   /* private void readSP(){
        String tel = sp.getString("tel",null);
        String pswd = sp.getString("password",null);
        Boolean remember = sp.getBoolean("remember",false);
        if (remember){
            username.setText(tel);
            userpass.setText(pswd);
            save_data.setChecked(remember);
        }
    }
    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", userpass.getText().toString());
        editor.commit();//提交修改
    }*/
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==RESULT_OK) {
            String user_name = data.getStringExtra("username");
            username.setText(user_name);
        }


    }



}
